<?php
/**
 * 
 * 根据话题抓取微博数据
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); $_SERVER = array_merge($_SERVER, $array_config); 
}


/*ini_set("display_errors",ture);
error_reporting(E_ALL);*/

define("CRON_NAME","cron_get_mblog_by_topic");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
$_GET["api_debug"] = true;

//15分钟一次,执行1000条
define("PCOUNT",10);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());



include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");

class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
		
		
		$this->after_run();
	}
	
	
	function get_data()
	{
		
		$db_mblog_cron_topic = new db_mblog_cron_topic();
		$mblog_from_api = new mblog_from_api();
		
		//获取tag的总数
		$count = $db_mblog_cron_topic->cron_get_mblog_topic_count($this->start_id);
		
		if($count == 0)
		{
			//如果获取不到需要更新的数据，初始化cron_start_id
			$this->ini_start_id(NOW_PROCESS);
			
			$this->start_id = 0 ;
			$count = $db_mblog_cron_topic->cron_get_mblog_topic_count($this->start_id);
		}
		
		
		
		echo "tag_count:$count<br>\n";
		$total_page = ceil($count/PCOUNT);
		if($total_page > MAX_PAGE && defined("MAX_PAGE")) $total_page = MAX_PAGE;
			
		for($i=1;$i<=$total_page;$i++)
		{
			$this->get_list_by_page($i);
		}
		
		
	}	
	
	function get_list_by_page($p)
	{
		$db_mblog_cron_topic = new db_mblog_cron_topic();
		
		//分批次取tag名称
		$food_tag = $db_mblog_cron_topic->cron_get_mblog_topic_list($this->start_id,$p,PCOUNT);
		
		var_dump($food_tag);
		foreach($food_tag as $value)
		{
			$topic = $value["topic"];
			$id = $value["id"];
			$cron_start_time  = $value["cron_start_time"];
			if($id % TOTAL_PROCESS != NOW_PROCESS)//用总进程做控制 example: 3个进程  php xxx 1 只会执行除3余1的id
			{
				continue;
			}
			
			if($topic)//处理抓取分页逻辑
			{
				$this->get_list_by_topic($topic,$id,1,$cron_start_time);
				
			}
			
			if($id)//更新抓取的start_id到数据库
			{
				$done_job = true;
				$res = $this->replace_into_data($id);	
			}
			
			
		}
		
		if($done_job == false)
		{
			$this->ini_start_id();
		}
			
	}
	
	
	//根据话题抓取数据并入库
	function get_list_by_topic($topic,$id,$p=1,$starttime="")
	{
		global $mid_have_get;
		$max_time = "";
		//执行时间超过30分钟，退出
		if(time() - CRON_START_TIME > 900)
		{
			exit;
		}
		$db_base = new db_base();
		$mblog_from_api = new mblog_from_api();
		
		$sql_status_tag = "insert ignore into mblog_cron_content ( id,content,pic,pic_width_pubu, pic_height_pubu,topic_id,topic,source,location,coordinates,create_uid,create_nickname,create_time,create_ip,status) values ";
		$sql_status_tag_array = array();
		
		if($topic)
		{
			
			if(!$starttime)
			{
				$starttime = strtotime($starttime["create_time"]);
				
			}
			
			//根据话题抓取
			$mblog_list = $mblog_from_api->api_get_mblog_by_topic($topic,$ids="",$sort="",$fuid="",$province="",$city="",$starttime,$endtime="",
				$hasori=1,
				$hasret="",
				$hastext="",
				$haspic=1,
				$hasvideo="",
				$hasmusic="",
				$haslink="",
				$hasat="",
				$hasv="",
				$istag="",
				$dup="",
				$antispam="",
				$base_app=0,
				$p,
				$pcount=50
				);
			echo "{$topic}:count:{$mblog_list["data"]["count"]}|get_count".count($mblog_list["data"]["result"]).":p:{$p}<br>\n";	
			
			
			if($mblog_list["data"]["result"])
			{
				foreach($mblog_list["data"]["result"] as $value1)
				{
					echo "time_compare:".strtotime($value1["created_at"])." == {$starttime}<br>\n";
					
					//如果时间跟最大时间相同，跳过
					if(strtotime($value1["created_at"]) == $starttime)
					{
						continue;
					}
					$u_time = strtotime($value1["created_at"]);
					if($max_time < $u_time) $max_time = $u_time;
					$create_time = date("Y-m-d H:i:s",$u_time);
					
					
					//获取微博坐标信息
					$coordinates = "";
					if($value1["geo"]["coordinates"])
					{
						$coordinates = $value1["geo"]["coordinates"][1].",".$value1["geo"]["coordinates"][0];
					}
					//获取坐标对应地址
					if($coordinates)
					{
						 $address_info = $mblog_from_api->api_geo_to_address($coordinates);
						if($address_info["data"]["result"])
						{
							$location = $address_info["data"]["result"][0]["province_name"]." ".$address_info["data"]["result"][0]["city_name"]." ".$address_info["data"]["result"][0]["address"];
						} 
					}
					$res = imagecreatefromjpeg($value1["bmiddle_pic"]);
					$srcH = ImageSY($res); 
					$pic_width_pubu = 200;
					$pic_height_pubu = intval($srcH/(440/220));
					//$split_word = $base_search->get_mblog_word_cut($value1["text"]);
					$value1["text"] = mysql_escape_string($value1["text"]);
					$sql_status_tag_array[] = "({$value1["mid"]}, '{$value1["text"]}' ,'{$value1["bmiddle_pic"]}',$pic_width_pubu, $pic_height_pubu,{$id},'{$topic}','{$value1["source"]}','{$location}','{$coordinates}',{$value1["user"]["id"]},'{$value1["user"]["screen_name"]}' , '{$create_time}','0',1)";
						
					
					if(count($sql_status_tag_array) > 1 )
					{
						if($sql_status_tag_array)
						{
							$sql  = $sql_status_tag. join(",",$sql_status_tag_array);
							$db_base->query($sql);
							unset($sql_status_tag_array);
						}
						
					}
				
				}
		
			}
				
		}
		
		
		if($p == 1)//当为第一页时，检查是否有多页需要抓取
		{	//最大页数为20，接口限制
			$total_page = ceil($mblog_list["data"]["count"]/50);
			$total_page = $total_page > 19 ? 19 : $total_page;
			echo "topic_page_count:{$total_page}<br>\n";
			
			for($i = 2 ; $i <= $total_page;$i++)
			{
				$this->get_list_by_topic($topic,$id,$i,$starttime);
			}
			
			if($max_time)
			{
				$sql ="update mblog_cron_topic  set cron_start_time = {$max_time} where id ={$id}";
				$db_base->query($sql);
			}
			
			
		}
			
	}
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
